package cn.tedu.controller;

import cn.tedu.controller.BaseAndEx.BaseController;
import cn.tedu.entity.Goods;
import cn.tedu.service.port.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    @Resource
    private GoodsService goodsService;

    /**
     * 根据商品分类获取商品列表
     * @param categoryId    商品id
     * @param orderBy       分类模式
     * @param page          当前页数
     * @return              商品分类列表
     */
    @RequestMapping("/list.do")
    public String getGoodsListByCategoryId(ModelMap modelMap,
            @RequestParam(value = "categoryId", required = true) Integer categoryId,
            @RequestParam(value = "orderBy", required = false) Integer orderBy,
            @RequestParam(value = "page", required = true) Integer page){
        //声明需要转发的数据
        String orderByStr;
        List<Goods> goodsList;
        Integer goodsCount;
        Integer pages;
        Integer countPerPage;

        if (categoryId == null || categoryId < 1 ){
            modelMap.addAttribute("msg", "商品分类请求参数有误!");
            return "error";
        }
        if (orderBy == null || orderBy < 0 || orderBy >= goodsService.ORDER_BY.length){
            orderBy = 0;
        }
        //获取总商品数
        goodsCount = goodsService.getGoodsCountByCategoryId(categoryId);
        if (page == null || page< 1){
            page = 1;
        }
        orderByStr = goodsService.ORDER_BY[orderBy];
        goodsList = goodsService.getGoodsListByCategoryId(
                categoryId, orderByStr, page);
//        //测试获取数据
//        for (Goods goods:goodsList) {
//            System.out.println("\t" + goods);
//        }
        //设置每页显示数 goodsService.COUNT_PER_PAGE
        goodsService.setCountPerPage(4);

        //获取每页显示数
        countPerPage = goodsService.getCountPerPage();

        //总分页数
        pages = goodsCount / countPerPage;
        if (pages % 20 != 0){
            pages++;
        }
        System.out.println("goodsCount"+goodsCount);
        System.out.println("countPerPage"+ countPerPage);
        System.out.println("pages"+pages);

        modelMap.addAttribute("goodsList", goodsList);
        modelMap.addAttribute("goodsCount", goodsCount);
        modelMap.addAttribute("countPerPage", countPerPage);
        modelMap.addAttribute("pages", pages);
        modelMap.addAttribute("page", page);
        modelMap.addAttribute("categoryId", categoryId);
        return "goods_list";
    }

    /**
     * 根据商品id获取商品信息
     * @param id        商品id
     * @param modelMap  存放商品信息
     * @return          返回商品信息
     */
    @RequestMapping("/details.do")
    public String showGoodsDetails(
            @RequestParam(value = "id", required = true) Integer id,
            ModelMap modelMap){
        if (id <= 0){
            return "error";
        }
        Goods goods = goodsService.getGoodsById(id);
        if (goods == null){
            return "error";
        }
        //获取同类商品
        List<Goods> goodsList = goodsService.getGoodsListByItemType(goods.getItemType());

        modelMap.addAttribute("goods", goods);
        modelMap.addAttribute("goodsList", goodsList);
        return "goods_details";
    }

}
